//
//  ProductManagerVC.m
//  LazyCat
//
//  Created by linhui on 15/12/24.
//  Copyright (c) 2015年 zhanshu. All rights reserved.
//

#import "ProductManagerVC.h"
#import "TabObjectProduct.h"
#import "ClassifyManageViewController.h"
@interface ProductManagerVC ()<UIPageViewControllerDataSource,UIPageViewControllerDelegate,UIScrollViewDelegate>
{
    UIPageViewController *_pageViewCtrl;
    NSMutableArray *_dataArray;
    NSInteger _currentPage;
    UIView *_bgView;
    UIView *_lineView;
}
@end

@implementation ProductManagerVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createUI];
    [self configPageViewController];
}
- (void)createUI{
    self.view.backgroundColor = BackColor;
    [self initNav:@"产品管理" doBack:YES];
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 63, ScreenWidth, 1)];
    lineView.backgroundColor = [UIColor lightGrayColor];
    lineView.alpha = 0.2;
    [self.navBar addSubview:lineView];
    //分类按钮
    UIButton *searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    searchBtn.frame = CGRectMake(ScreenWidth - 52, 30, 42, 30);
    
    [searchBtn addTarget:self action:@selector(categoryBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [searchBtn setTitle:@"分类" forState:UIControlStateNormal];
    [searchBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    searchBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [searchBtn setImageEdgeInsets:UIEdgeInsetsMake(4, 10, 4, 10)];
    [self.navBar addSubview:searchBtn];
    _bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 64, ScreenWidth, 40)];
    _bgView.backgroundColor =[UIColor whiteColor];
    [self.view addSubview:_bgView];
    _bgView.alpha = 1;
    UIView *buttomLine = [[UIView alloc]initWithFrame:CGRectMake(0, 104, ScreenWidth, 1)];
    buttomLine.backgroundColor = BackColor;
    buttomLine.alpha = 0.2;
    [self.view addSubview:buttomLine];
    _dataArray = [[NSMutableArray alloc]init];
    NSArray *array;
    
    array = @[@"已上架",@"已下架"];
    for(int i=0;i<array.count;i++){
        TabObjectProduct *tab = [[TabObjectProduct alloc]init];
        tab.title = array[i];
        tab.subViewController = [[SubProductManagerVC alloc]init];
        tab.subViewController.currentPage = i;
        [_dataArray addObject:tab];
        __weak typeof(self)wSelf = self;
        [tab.subViewController setMyBlock:^(UIViewController *detailVC) {
            [wSelf.navigationController pushViewController:detailVC animated:YES];
        }];
    }
    _currentPage = 0;
    _lineView = [[UIView alloc]init];
    _lineView.backgroundColor = SetColor(255,179, 14, 1);
    [_bgView addSubview:_lineView];
    for(int i=0;i<_dataArray.count;i++){
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(i*(ScreenWidth/_dataArray.count), 0, ScreenWidth/_dataArray.count, 40);
        [btn setTitle:array[i] forState:UIControlStateNormal];
        [btn setTitleColor:SetColor(51, 51, 51, 1) forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:14];
        [btn setTitleColor:SetColor(255,179, 14, 1) forState:UIControlStateSelected];
        [btn setBackgroundColor:[UIColor clearColor]];
        [btn addTarget:self action:@selector(changeTab:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag = 100 + i;
        [_bgView addSubview:btn];
        if(i==_currentPage){
            btn.selected = YES;
            _lineView.frame = CGRectMake(i*(ScreenWidth/2)+40, 38, ScreenWidth/2-80, 2);
        }
    }
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self   action:@selector(hideKeyB)];
    [self.navBar addGestureRecognizer:tap];
}
- (void)hideKeyB {
    [self.view endEditing:YES];
}
//分类按钮点击
- (void)categoryBtnClick {
    ClassifyManageViewController *classman = [[ClassifyManageViewController alloc] init];
    [self.navigationController pushViewController:classman animated:YES];
}
- (void)configPageViewController{
    _pageViewCtrl = [[UIPageViewController alloc]initWithTransitionStyle:(UIPageViewControllerTransitionStyleScroll) navigationOrientation:(UIPageViewControllerNavigationOrientationHorizontal) options:nil];
    SubProductManagerVC * sub = [_dataArray[_currentPage] subViewController];
    
    [_pageViewCtrl setViewControllers:@[sub] direction:(UIPageViewControllerNavigationDirectionForward) animated:YES completion:^(BOOL finished) {
        
    }];
    _pageViewCtrl.view.frame = CGRectMake(0, 40+65, ScreenWidth, ScreenHeight-65-40);
    [self.view addSubview:_pageViewCtrl.view];
    _pageViewCtrl.dataSource = self;
    _pageViewCtrl.delegate = self;
    //获取显示所有view的滚动视图
    UIScrollView *sv = _pageViewCtrl.view.subviews[0];
    sv.delegate = self;
    [_pageViewCtrl.view.subviews[0] setDelegate:self];
}
//单击按钮时切换视图
- (void)changeTab:(UIButton *)sender {
    NSInteger index = sender.tag-100;
    SubProductManagerVC *sub = [_dataArray[index] subViewController];
    __weak typeof(_bgView)wBgView = _bgView;
    __weak typeof(_lineView)wLineView = _lineView;
    //显示翻页后要显示sub，翻页方向根据单击的按钮决定
    [_pageViewCtrl setViewControllers:@[sub] direction:_currentPage>index animated:YES completion:^(BOOL finished) {
        _currentPage = index;
        for(UIView *view in wBgView.subviews){
            if([view isKindOfClass:[UIButton class]]){
                UIButton *tb = (UIButton *)view;
                if(tb.tag == sender.tag){
                    tb.selected=YES;
                }else {
                    tb.selected = NO;
                }}}
        wLineView.frame = CGRectMake(sender.frame.origin.x+40, 38, ScreenWidth/2-80, 2);
    }];
}
#pragma mark --
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController{
    SubProductManagerVC *sub = (SubProductManagerVC *)viewController;
    NSInteger index = sub.currentPage;
    index++;
    if(index>=_dataArray.count){
        index = 0;
        return nil;
    }
    SubProductManagerVC *rsvc = [_dataArray[index]subViewController];
    return rsvc;
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController{
    SubProductManagerVC *sub = (SubProductManagerVC *)viewController;
    NSInteger index = sub.currentPage;
    index--;
    if(index<0){
        index = _dataArray.count-1;
        return nil;
    }
    SubProductManagerVC *rsvc = [_dataArray[index]subViewController];
    return rsvc;
    
}
//翻页成功后执行的方法
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed{
    SubProductManagerVC *sub = (SubProductManagerVC *)pageViewController.viewControllers[0];
    _currentPage = sub.currentPage;
    for(UIView *view in _bgView.subviews){
        if([view isKindOfClass:[UIButton class]]){
            UIButton *btn = (UIButton *)view;
            if(btn.tag-100==_currentPage){
                btn.selected =YES;
            }else {
                btn.selected = NO;
            }
        }
    }
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    CGFloat offSetX = scrollView.contentOffset.x;
    CGFloat ratio = (ScreenWidth - 80)/2/ScreenWidth;
    CGRect frame = _lineView.frame;
    frame.origin.x = (offSetX-ScreenWidth)*ratio+_currentPage*ScreenWidth/2+40;
    _lineView.frame = frame;
}
@end
